﻿using System.Data;
using Marbles.Core.Connectors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.BBS;
using Marbles.Core.Authentication;

namespace Marbles.Web.BBS
{
    public partial class ForumPostList : Marbles.Core.Content.UserControl
    {
        public string ForumName { get { object obj = ViewState["ForumName"]; return (obj == null) ? "" : obj.ToString(); } set { ViewState["ForumName"] = value; } }
        public Int64 PostId { get { object obj = ViewState["PostId"]; return (obj == null) ? 0 : Convert.ToInt64(obj); } set { ViewState["PostId"] = value; } }
        public Int32 PageSize { get { return cPager.PageSize; } set { cPager.PageSize = value; } }

        private Core.BBS.Forum forum = null;
        private bool isEditor = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            bool isEditor = this.Page.IsEditor || this.Page.IsAdministrator;
            Initialise();
            if (!Page.IsPostBack) { LoadData(); }
        }

        private void Initialise()
        {
            isEditor = this.Page.IsEditor || this.Page.IsAdministrator;
            forum = new Core.BBS.Forum(this.CurrentWeb.SiteId, ForumName);
            cLnkCategory.Text = forum.CategoryTitle;
            cLnkTitle.Text = forum.Title;
            cLnkTitle.NavigateUrl = "Forum.aspx?f=" + ForumName;
            cLnkCategory.NavigateUrl = "Default.aspx";
            cLblDescription.Text = forum.Description;
        }


        protected void cThreadList_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType != ListViewItemType.DataItem) { return; }

            DataRowView dr = ((ListViewDataItem)e.Item).DataItem as DataRowView;
            DateTime value = Convert.ToDateTime(dr["postdate"]);
            Label cLblPostDate = e.Item.FindControl("cLblPostDate") as Label;
            Button cBtnReply = e.Item.FindControl("cBtnReply") as Button;
            Button cBtnLike = e.Item.FindControl("cBtnLike") as Button;
            Button cBtnDelete = e.Item.FindControl("cBtnDelete") as Button;
            Button cBtnStick = e.Item.FindControl("cBtnStick") as Button;
            Button cBtnApproved = e.Item.FindControl("cBtnApproved") as Button;
            Button cBtnLock = e.Item.FindControl("cBtnLock") as Button;
            Panel cPnlAdmin = e.Item.FindControl("cPnlAdmin") as Panel;
            cLblPostDate.Text = (value.Year <= 1980) ? "" : value.ToString("ddd") + " " + value.ToShortDateString() + " " + value.ToShortTimeString();
            cBtnReply.OnClientClick = "window.location.href='Post.aspx?f=" + ForumName + "&t=" + this.PostId.ToString() + "';return false;";
            cBtnReply.CommandArgument = dr["postid"].ToString();
            cBtnLike.CommandArgument = dr["postid"].ToString();
            if (Convert.ToBoolean(dr["locked"])) { cBtnLock.Text = "Unlock"; cBtnLock.CommandName = "Unlock"; }
            if (Convert.ToBoolean(dr["sticky"])) { cBtnStick.Text = "UnStick"; cBtnStick.CommandName = "UnStick"; }
            if (Convert.ToBoolean(dr["approved"])) { cBtnApproved.Text = "UnApprove"; cBtnApproved.CommandName = "UnApprove"; }
            cBtnLock.CommandArgument = Convert.ToInt64(dr["postid"]).ToString();
            cBtnApproved.CommandArgument = Convert.ToInt64(dr["postid"]).ToString();
            cBtnStick.CommandArgument = Convert.ToInt64(dr["postid"]).ToString();
            cBtnDelete.CommandArgument = Convert.ToInt64(dr["postid"]).ToString();
            cPnlAdmin.Visible = cBtnApproved.Visible = cBtnStick.Visible = cBtnDelete.Visible = cBtnLock.Visible = isEditor;
            if (Convert.ToBoolean(dr["locked"])) { cBtnReply.Visible = false; }
        }

        private void LoadData()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM forumitems WHERE siteid = @siteid AND forumname = @forumname AND (postid = @postid OR parentpostid = @postid) AND approved = 1 ORDER BY sticky DESC, postdate ASC");

            cmd.AddParameter("@siteid", forum.Siteid);
            cmd.AddParameter("@forumname", forum.ForumName);
            cmd.AddParameter("@postid", this.PostId);
            cThreadList.DataSource = dbc.FetchDataTable(cmd);
            cThreadList.DataBind();
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            Int64 postid = 0;
            if (!string.IsNullOrEmpty(e.CommandArgument.ToString())) { postid = Convert.ToInt64(e.CommandArgument.ToString()); }

            if (postid == 0) { return; }

            Core.BBS.ForumPost post = new Core.BBS.ForumPost(this.CurrentWeb.SiteId, this.ForumName, postid);

            switch (e.CommandName.ToLower())
            {
                case "delete":
                    post.Delete();
                    break;
                case "lock":
                    post.LockThread();
                    break;
                case "unlock":
                    post.UnlockThread();
                    break;
                case "stick":
                    post.Sticky = true;
                    post.Update();
                    break;
                case "unstick":
                    post.Sticky = false;
                    post.Update();
                    break;
                case "unapprove":
                    post.UnApprovePost();
                    break;
                case "approve":
                    post.ApprovePost();
                    break;
                case "like":
                    post.Like();
                    break;
            }

            Response.Redirect("Discussion.aspx?f=" + ForumName + "&t=" + PostId.ToString(), true);
        }

    }

}